package com.feemanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Anonymous;
import com.common.core.domain.AjaxResult;
import com.common.core.domain.R;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.utils.StringUtils;
import com.common.utils.poi.ExcelUtil;
import com.common.validate.add;
import com.common.validate.edit;
import com.feemanage.domain.bo.BizCwIncomeClaimAllBo;
import com.feemanage.domain.bo.BizCwIncomeClaimBo;
import com.feemanage.domain.vo.BizCwIncomeClaimVo;
import com.feemanage.service.IcWIncomeClaimService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 项目入账
 */
@Api(tags="项目入账")
@RestController
@RequestMapping("/feemanage/cwincomeclaim")
public class cWIncomeClaimController {

    @Autowired
    private IcWIncomeClaimService baseService;

    /**
     * 查询列表
     * @param pageDomain
     * @param claim
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, BizCwIncomeClaimBo claim){
        IPage<BizCwIncomeClaimVo> page = baseService.selectList(pageDomain,claim);
        return TableDataInfo.build(page.getRecords(),page.getTotal());
    }

    /**
     * 检索表头统计
     * @param claim
     * @return
     */
    @ApiOperation("检索表头统计")
    @GetMapping(value = "/listtotal")
    public AjaxResult listtotal(BizCwIncomeClaimBo claim)
    {
        List<HashMap<String, Integer>> list =baseService.selectListTotal(claim);
        return AjaxResult.success(list);
    }


    @ApiOperation("导出项目入账")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PageDomain pageDomain,@RequestBody BizCwIncomeClaimBo claim)
    {
        List<BizCwIncomeClaimVo> list = baseService.selectExportList(pageDomain,claim);
        ExcelUtil<BizCwIncomeClaimVo> util = new ExcelUtil<>(BizCwIncomeClaimVo.class);
        util.exportExcel(response, list, "经费认领","经费认领");
    }


    /**
     * 新增
     * @param claim
     * @return
     */
    @PostMapping("/add")
    public R<Void> addCwIncomeclaim(@Validated(add.class) @RequestBody BizCwIncomeClaimAllBo claim){
        String msg = baseService.addCwIncomeclaim(claim);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 编辑
     * @param claim
     * @return
     */
    @PostMapping("/edit")
    public R<Void> editCwIncomeclaim(@Validated(edit.class) @RequestBody  BizCwIncomeClaimAllBo claim){
        String msg = baseService.editCwIncomeclaim(claim);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove( @NotEmpty(message = "主键不能为空") @PathVariable String[] ids)
    {
        boolean result = baseService.delCwIncomeclaim(Arrays.asList(ids),true);
        return result ? R.ok() : R.fail();
    }



}
